package com.zzptc.myapp1211.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zzptc.myapp1211.R;
import com.zzptc.myapp1211.databinding.RvItemBinding;
import com.zzptc.myapp1211.entity.ResultsBean;

import org.litepal.LitePal;

import java.util.List;

public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.ViewHolder> { //这里点三次 ait+回车 都点第一个

    private final List<ResultsBean> resultsBeans;
    private final Context mContext;



    public MyRvAdapter(Context mContext) {
       resultsBeans= LitePal.findAll(ResultsBean.class);//这个方法体=数据库框架.找到所有的这个数据库记录

        this.mContext = mContext;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        //LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item,parent,false);

       RvItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),//布局的
                R.layout.rv_item, parent, false);

        return new ViewHolder(binding); //inflate=binding

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //resultBeans.get(position).var
        ResultsBean resultBean = resultsBeans.get(position);
        holder.binding.tvTitle.setText(resultBean.getTitle());

        Glide.with(mContext).load(resultBean.getCover()).into(holder.binding.ivCover);



    }

    @Override
    public int getItemCount() {
        return resultsBeans.size();//列表的长度 确定展示多少项？
    }

    public class ViewHolder extends RecyclerView.ViewHolder { //这里也是alt+回车
        private final RvItemBinding binding;

        public ViewHolder(@NonNull RvItemBinding binding) {
            super(binding.getRoot());
            this.binding=binding;                                 //的绑定

        }

    }
}
